﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoVanHocTapData.Entities;

namespace CoVanHocTapProject.Controllers
{
    public class ChatterbotController : Controller
    {
        
        
        #region du lieu chat
        List<LogChatter> dataList = new List<LogChatter>();

        string delim = "?!.;,";

        #endregion

        public ActionResult Index()
        {
            return View();
        }

        static LogChatter chatEntity = new LogChatter();

        [HttpPost]
        public ActionResult Index(FormCollection model)
        {
            
            var submit = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("btnSend"));
            if (submit != null)
            {
                
                string strInput = model["strInput"];
                dataList.Add(new LogChatter { Input = "What is your name", Response = new List<string>(){ "My name is Chatbot","you can call me Chatbot"} });
                dataList.Add(new LogChatter { Input = "Hi", Response = new List<string>() { "Hi", "How are you","Hi there!" } });

                if (FindMatch(strInput) != null)
                {

                }

                chatEntity.Output += "SV> " + strInput + "<br/>";
                //int nSelection = new Random().Next(Response.Length);
                //chatEntity.Output += "GVCV> " + Response[nSelection]+"<br/>";
            }
            return View(chatEntity);
        }

        List<string> FindMatch(string _input)
        {
            List<string> result;
            for (int i = 0; i < dataList.Count; i++)
            {
                if (dataList[i].Input.Equals(_input))
                {
                    result = dataList[i].Response;
                    return result;
                }
            }
            return null;
        }

        void preprocess_input( string str ) 
        {
	        cleanString(str);
            str = str.ToUpper();
        }

        bool isPunc(char c)  //ktra c co nam trong cac ky tu cua delim ko
        {
            for (int i = 0; i < delim.Length; i++)
            {
                if (delim[i] == c)
                    return true;
            }
            return false;
        }

        // removes punctuation and redundant
        // spaces from the user's input
        void cleanString(string str ) 
        {
	        int len = str.Length;
	        string temp = "";

	        char prevChar = '0';

	        for(int i = 0; i < len; ++i) 
	        {
		        if(str[i] == ' ' && prevChar == ' ') 
		        {
			        continue;
		        }

		        else if(!isPunc(str[i])) 
		        {
			        temp += str[i];
		        }

		        prevChar = str[i];
	        }
	        str = temp;
        }

    }
}
